package com.example.helper2;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.CheckBox;

import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

public class fragment_login extends DialogFragment {

    private EditText user_name;
    private EditText user_word;
    private EditText user_phone;
    private EditText user_address;
    private CheckBox remember;
    private Button login;

    public View onCreateView(LayoutInflater inflater, android.view.ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.login_form,container,false);
        initView(view);

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName = user_name.getText().toString();
                String userWord = user_word.getText().toString();
                String userPhone = user_phone.getText().toString();
                String userAddress = user_address.getText().toString();
                try {
                    SharedPreferences sharedPreferences = getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.clear();
                    if (remember.isChecked()){
                        editor.putString("rem_name",userName);
                        editor.putString("rem_word",userWord);
                        editor.putString("rem_phone",userPhone);
                        editor.putString("rem_address",userAddress);
                    }
                    editor.putString("user_name",userName);
                    editor.putString("user_word",userWord);
                    editor.putString("user_phone",userPhone);
                    editor.putString("user_address",userAddress);
                    editor.apply();
                    Toast.makeText(getActivity(),"登录成功！",Toast.LENGTH_SHORT).show();
                    if(getActivity() != null){
                        ((MainActivity) getActivity()).initLoginBtn();
                    }
                    // 当输入框中没有字符时，将不隐藏输入框
                    // 直到所有的输入框中都有值，隐藏输入框
                    // 因为不确定用户最后输入是停留在哪个输入框，所以全都需要判断一下
                    InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (user_name.getText().toString().length() != 0){
                        imm.hideSoftInputFromWindow(user_name.getWindowToken(),0);
                    }
                    if (user_word.getText().toString().length() != 0){
                        imm.hideSoftInputFromWindow(user_word.getWindowToken(),0);
                    }
                    if (user_phone.getText().toString().length() != 0){
                        imm.hideSoftInputFromWindow(user_phone.getWindowToken(),0);
                    }
                    if (user_address.getText().toString().length() != 0){
                        imm.hideSoftInputFromWindow(user_address.getWindowToken(),0);
                    }
                    dismiss();
                }catch (Exception e){
                    Toast.makeText(getActivity(),"登录失败！",Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }
        });

        return view;
    }

    // 绑定组件
    private void initView(View view){
        user_name = view.findViewById(R.id.user_name);
        user_word = view.findViewById(R.id.user_word);
        user_phone = view.findViewById(R.id.user_phone);
        user_address = view.findViewById(R.id.user_address);
        remember = view.findViewById(R.id.remember);
        login = view.findViewById(R.id.login);
        try{
            SharedPreferences sharedPreferences = getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
            String rem_name = sharedPreferences.getString("rem_name",null);
            if (rem_name == null || rem_name.length() == 0){
                return;
            }
            String rem_word = sharedPreferences.getString("rem_word",null);
            String rem_phone = sharedPreferences.getString("rem_phone",null);
            String rem_address = sharedPreferences.getString("rem_address",null);

            user_name.setText(rem_name);
            user_word.setText(rem_word);
            user_phone.setText(rem_phone);
            user_address.setText(rem_address);
            remember.setChecked(true);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
